# -*- coding: utf-8 -*-

# @Author: Chen Hua
# @Date  : 2018/12/28
# @Desc  : 日期格式转换类

from datetime import datetime
from util import typeassert

"""
将  yyyyMMdd 这样格式的日期字符串
转换为 date 类型
"""


# @typeassert(dtStr=str)
def yyyyMMhh_str_to_date(dtStr):
    try:
        if not dtStr:
            return None
        else:
            rstDt = datetime.strptime(dtStr, '%Y%m%d')
            return rstDt
    except Exception as e:
        print("字符转日期失败,", e)


def yyyy_MM_hh_str_to_date(dtStr):
    try:
        if not dtStr:
            return None
        else:
            rstDt = datetime.strptime(dtStr, '%Y-%m-%d')
            return rstDt
    except Exception as e:
        print("字符转日期失败,", e)


# @typeassert(dt=datetime)
def date_to_yyyyMMhh_str(dt):
    try:
        if not datetime:
            return None
        else:
            rstStr = dt.strftime('%Y%m%d')
            return rstStr
    except Exception as e:
        print("日期转字符失败,", e)


# @typeassert(dt=datetime)
def date_to_yyyy_MM_hh_str(dt):
    try:
        if not datetime:
            return None
        else:
            rstStr = dt.strftime('%Y-%m-%d')
            return rstStr
    except Exception as e:
        print("日期转字符失败,", e)


if __name__ == "__main__":
    rst = yyyyMMhh_str_to_date("20181212")
    print(rst)
    print(type(rst))
    strrst = date_to_yyyyMMhh_str(datetime.now())
    print(strrst)
    print(type(strrst))
